from django.db import models

class Department(models.Model):
    """ 部门表 """
    # id会自动创建
    # id = models.AutoField(verbose_name="ID",primary_key=True)
    # id = models.BigAutoField(verbose_name="ID",primary_key=True)
    title = models.CharField(verbose_name="标题", max_length=64)

    def __str__(self):
        return self.title

# Create your models here.
class UserInfo(models.Model):
    """ 员工表 """
    name = models.CharField(verbose_name="姓名", max_length=32)
    password = models.CharField(verbose_name="密码", max_length=64)
    age = models.IntegerField(verbose_name="年龄", default=None)
    account = models.DecimalField(verbose_name="账户余额", max_digits=10, decimal_places=2, default=None)
    # creatime = models.DateTimeField()
    creatime = models.DateField(verbose_name="入职时间")
    gender_choice = (
        (1,'男'),
        (2,'女')
    )
    gender = models.SmallIntegerField(verbose_name="性别", choices=gender_choice)

    # 无约束
    # departMentId = models.BigIntegerField(verbose_name="部门ID")

    # 1.有约束
    #  - to：与哪张表关联
    #  - to_field：表中的哪一列关联
    # 2.Django自动
    #  - 写的depart
    #  - 生成数据库的列 depart_id
    # 3.部门表被删除
    #  3.1 级联删除
    #    testdepart = models.ForeignKey(to="Department", to_field="id", on_delete=models.CASCADE)
    #  3.2 置空
    #    testdepart = models.ForeignKey(to="Department", to_field="id",
    #                                   null=True, blank=True, on_delete=models.SET_NULL)
    depart = models.ForeignKey(verbose_name="部门", to="Department", to_field="id",
                               null=True, blank=True, on_delete=models.SET_NULL)


class PrettyNum(models.Model):
    """ 靓号表 """
    mobile = models.CharField(verbose_name="手机号", max_length=11)
    # 想要参数允许为空，null=Ture,blank=Ture
    price = models.IntegerField(verbose_name="价格", default=1)

    level_choice = {
        (1, "1级"),
        (2, "2级"),
        (3, "3级"),
        (4, "4级"),
    }
    level = models.SmallIntegerField(verbose_name="级别", choices=level_choice, default=1)

    ststus_choice = {
        (1, "已占用"),
        (2, "未使用"),
    }
    status = models.SmallIntegerField(verbose_name="状态", choices=ststus_choice, default=2)

class TestUser(models.Model):
    """ 员工表 """
    name = models.CharField(verbose_name="姓名", max_length=32)
    password = models.CharField(verbose_name="密码", max_length=64)
    age = models.IntegerField(verbose_name="年龄", default=None)

    class Meta:
        db_table = "test_user"  # 自定义表名

    def __str__(self):
        return self.name

class Admin(models.Model):
    """管理员"""
    username = models.CharField(verbose_name="用户名", max_length=32)
    password = models.CharField(verbose_name="密码", max_length=64)

    def __str__(self):
        return self.username

class Task(models.Model):
    """ 任务
    主要为了测试ajax的使用
    """
    level_choice = {
        (1, '特提'),
        (2, '平急'),
        (3, '一般'),
    }
    level = models.SmallIntegerField(verbose_name='级别', choices=level_choice, default=3)
    title = models.CharField(verbose_name='标题', max_length=64)
    detail = models.TextField(verbose_name='详细信息')
    # CASCADE 级联删除
    # to 关联表
    # to_field 可不用写
    user = models.ForeignKey(verbose_name='任务负责人', to='Admin', to_field='id', on_delete=models.CASCADE)

class Order(models.Model):
    """订单"""
    oid = models.CharField(verbose_name='订单号', max_length=64)
    title = models.CharField(verbose_name='商品名称', max_length=64)
    price = models.IntegerField(verbose_name='商品价格')
    status_choice = {
        (1, '待支付'),
        (2, '已支付')
    }
    status = models.SmallIntegerField(verbose_name='状态', choices=status_choice, default=1)
    user = models.ForeignKey(verbose_name='订单用户', to=Admin, on_delete=models.CASCADE, default=1)

# class TTestApiWebUser(models.Model):
#     num = models.CharField(verbose_name='用户编码', max_length=64)
#     domain = models.CharField(verbose_name='领域', max_length=64)
#     operation_user = models.CharField(verbose_name='操作人账号', max_length=64)
#     operation_user_name = models.CharField(verbose_name='操作人姓名', max_length=64)
#     position = models.CharField(verbose_name='位置', max_length=64)
#     create_by = models.CharField(verbose_name='创建人', max_length=64)
#     create_date = models.CharField(verbose_name='创建时间', max_length=64)
#     last_update_by = models.CharField(verbose_name='更新人', max_length=64)
#     last_update_date = models.CharField(verbose_name='更新时间', max_length=64)
#     class Meta:
#         db_table = "t_test_api_web_user"  # 自定义表名

# 本质：
# """
# create table tools_userinfo(
#     id bigint auto_increment primary key,
#     name varchar(32),
#     password varchar(64),
#     age int
# )
# """

# 执行命令:
# python manage.py makemigrations
# python manage.py migrate

# 删除表/字段：注掉代码，再执行上述命令

# ########## 新建数据 ##########  代码写到views.py
# 本质：insert into tools_department(title) values ("销售部");
# Department.objects.create(title="销售部")
# UserInfo.objects.create(name="zhangsan", password="123456", age=30)


# models.py
class Product(models.Model):
    name = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    category = models.CharField(max_length=50)

    def __str__(self):
        return self.name





class Product2(models.Model):
    # name = models.CharField(max_length=100)
    # category = models.CharField(max_length=50)
    # price = models.DecimalField(max_digits=10, decimal_places=2)
    # description = models.TextField(blank=True)
    # created_at = models.DateTimeField(auto_now_add=True)
    #
    # def __str__(self):
    #     return self.name
    name = models.CharField(max_length=200, verbose_name="产品名称")
    category = models.CharField(max_length=100, verbose_name="产品分类")
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="产品价格")
    description = models.TextField(verbose_name="产品描述")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "产品"
        verbose_name_plural = "产品"
        ordering = ['-created_at']